package com.webside.advertisement.controller;
 
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.webside.advertisement.model.AdPositionEntity;
import com.webside.advertisement.model.AdReleaseEntity;
import com.webside.advertisement.service.AdReleaseService;
import com.webside.base.basecontroller.BaseController;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.exception.AjaxException;
import com.webside.exception.ServiceException;
import com.webside.exception.SystemException;
import com.webside.util.Common;
import com.webside.util.PageUtil;
 

@Controller
@Scope("prototype")
@RequestMapping("/adarrange/")
public class AdArrangeController extends BaseController {

	@Autowired
	private AdReleaseService adreleaseService;
	
	@RequestMapping("listUI.html")
	public String listUI(Model model, HttpServletRequest request,Long id) {
		try
		{
			PageUtil page = new PageUtil();
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			
			model.addAttribute("ids", id);
			model.addAttribute("page", page);
			return Common.BACKGROUND_PATH + "/adarrange/list";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	
	
	
	
	@RequestMapping("querymonth.html")
	@ResponseBody
	public Object querymonth(String year,String month) throws Exception{
		Map<String, List<String>> hashMaps =new HashMap<String, List<String>>();
		int years=Integer.parseInt(year);
		int months=Integer.parseInt(month);
		List<AdPositionEntity> list=adreleaseService.queryAllAdPositionCode();
		Calendar calendar=Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, years);
		calendar.set(Calendar.MONTH,months);
		int maxdate=calendar.getActualMaximum(Calendar.DATE);
		
		
		for(int j=0;j<list.size();j++){
			System.out.println(list.get(j).getName()+"||"+list.get(j).getCode()+"||"+list.size());
			hashMaps.put(list.get(j).getName(),new ArrayList<String>());
			String date;
			if(months<11){
				 date=year+"-0"+(months+1);
			}else{
				 date=year+"-"+(months+1);
			}
			String edate=year+"-"+(months+1)+"-"+maxdate+" -23:59:59";
			//查询出其月份及相对货位的所有投放广告；
			List<AdReleaseEntity> subTime=adreleaseService.queryListByAdPAnddate(list.get(j).getCode(), date, edate, "");
			List<Date> sdates=new ArrayList<Date>();
			List<Date> eDates=new ArrayList<Date>();
			SimpleDateFormat format;
			System.out.println(maxdate);
			if(subTime.size()>0){
				for(int t=1;t<=maxdate;t++){
					String datestring;
					format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					if(months<11){	
						if(t<10){
							datestring=year+"-0"+(months+1)+"-0"+t+" 00:00:00";
						}else{
							datestring=year+"-0"+(months+1)+"-"+t+" 00:00:00";
						}
					}else{
						if(t<10){
							datestring=year+"-"+(months+1)+"-0"+t+" 00:00:00";
						}else{
							datestring=year+"-"+(months+1)+"-"+t+" 00:00:00";
						}
					}
					Date date1=format.parse(datestring);
					
					sdates.add(date1);
					if(months<11){
						if(t<10){
							datestring=year+"-0"+(months+1)+"-0"+t+" 23:59:59";
						}else{
							datestring=year+"-0"+(months+1)+"-"+t+" 23:59:59";
						}
					}else{
						if(t<10){
							datestring=year+"-"+(months+1)+"-0"+t+" 23:59:59";
						}else{
							datestring=year+"-"+(months+1)+"-"+t+" 23:59:59";
						}
					}
					Date date2=format.parse(datestring);

					eDates.add(date2);
					for(int i=0;i<subTime.size();i++){
						for(int k=0;k<eDates.size();k++){
							//如果开始时间小于等于初时间；
							if(subTime.get(i).getStartTime().before(sdates.get(k)) || subTime.get(i).getStartTime().equals(sdates.get(k))){
								//如果结束时间小于末时间；
								if(subTime.get(i).getEndTime().before(eDates.get(k)) && subTime.get(i).getEndTime().after(sdates.get(k))){
									sdates.remove(k);
									sdates.add(subTime.get(i).getEndTime());
									//如果大于一排序
									if(sdates.size()>1){
										Collections.sort(sdates);
									}
									//如果结束时间大于等于
								}else if(subTime.get(i).getEndTime().equals(eDates.get(k)) || subTime.get(i).getEndTime().after(eDates.get(k))){		
										sdates.remove(k);
										eDates.remove(k);
										if(sdates.size()>1){
											Collections.sort(sdates);
										}
										if(eDates.size()>1){
											Collections.sort(eDates);
										}
										k--;
									
								}
								
							}else if(subTime.get(i).getStartTime().after(sdates.get(k))){ 
								if(subTime.get(i).getStartTime().before(eDates.get(k))){
									if(subTime.get(i).getEndTime().before(eDates.get(k))){
										sdates.add(subTime.get(i).getEndTime());
										eDates.add(subTime.get(i).getStartTime());
										if(sdates.size()>1){
											Collections.sort(sdates);
										}
										if(eDates.size()>1){
											Collections.sort(eDates);
										}
										k--;
									}else if(subTime.get(i).getEndTime().equals(eDates.get(k)) || subTime.get(i).getEndTime().after(eDates.get(k))){
										eDates.remove(k);
										eDates.add(subTime.get(i).getStartTime());
										if(eDates.size()>1){
											Collections.sort(eDates);
										}
										
									}
								}
								
							}
						}
					}
					long sum1=0;
					for(int n=0;n<sdates.size();n++){
						sum1=sum1+eDates.get(n).getTime()-sdates.get(n).getTime();
					}
					
					if(sum1>=3600000 && sum1<86399000){
						hashMaps.get(list.get(j).getName()).add("class='bghalf' code="+list.get(j).getCode());
					}else if(sum1<3600000){
						hashMaps.get(list.get(j).getName()).add("class='bgall' code="+list.get(j).getCode());
					}else{
						hashMaps.get(list.get(j).getName()).add("class='nothing' code="+list.get(j).getCode());
					}
					sdates.clear();
					eDates.clear();
				}
				
			}else{
				for(int t=1;t<=maxdate;t++){
					hashMaps.get(list.get(j).getName()).add("class='nothing' code="+list.get(j).getCode());
				}
				
		}
		
	
	
	}
		
		return hashMaps;
		
	}
	//添加显示
	@RequestMapping("addUI.html")
	public String addUI(Model model, HttpServletRequest request) {
	

		return Common.BACKGROUND_PATH + "/adarrange/form";
	}
	//添加
	@RequestMapping("add.html")
	@ResponseBody
	public Object add(AdReleaseEntity dictionary)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		

		try
		{
			int count=adreleaseService.insert(dictionary);
			 
			if(count >0 )
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "添加成功");
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "添加失败");
			}
		}catch(ServiceException e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//修改显示
	@RequestMapping("editUI.html")
	public String editUI(Model model, HttpServletRequest request, Long id) {
		
		try
		{
			AdReleaseEntity dictionary=adreleaseService.findById(id);
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
		
			model.addAttribute("page", page);
			model.addAttribute("adreleaseentity", dictionary);

			return Common.BACKGROUND_PATH + "/adarrange/form";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	//修改
	@RequestMapping("edit.html")
	@ResponseBody
	public Object update(AdReleaseEntity dictionaryIds)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		
		try
		 {  	 
				
				int result = adreleaseService.update(dictionaryIds);
				System.out.println(result);
			if(result >0)
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "编辑成功");
				
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "编辑失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//删除
	@RequestMapping("deleteBatch.html")
	@ResponseBody
	public Object deleteBatch(String ids) {
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = adreleaseService.deleteBatchById(list);
			System.out.println(cnt);
			if(cnt == list.size())
			{
				result.put("success", true);
				result.put("data", null);
				result.put("message", "删除成功");
			}else
			{
				result.put("success", false);
				result.put("data", null);
				result.put("message", "删除失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return result;
	}
	
	
 
	
}
